package com.charonmc.commands;

import com.charonmc.CharonCore;
import com.charonmc.utils.BookUtil;
import com.charonmc.utils.bookutils.BookHelper;
import com.charonmc.utils.mysql.SqlManager;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.sql.ResultSet;

public class Nick implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (player.hasPermission("Core.Nick")) {
                if (args.length == 0) {
                    BookHelper.openPlayer((Player)sender, BookUtil.getNickBook());
                }
                else if (args.length == 1) {
                    if (args[0].equals("own")){
                        player.sendMessage("§b§l请使用/nick <name>");
                    } else {
                        BukkitRunnable addPlayerNick = new BukkitRunnable() {
                            @SneakyThrows
                            @Override
                            public void run() {
                                SqlManager SqlManager = new SqlManager("charonmc");
                                Object id = SqlManager.createQuery()
                                        .inTable("players")
                                        .selectColumns("player_id")
                                        .addCondition("player_name", player.getName())
                                        .build()
                                        .execute()
                                        .first();
                                ResultSet set = SqlManager.createQuery()
                                        .inTable("nick")
                                        .selectColumns("player_id")
                                        .addCondition("player_id", id)
                                        .build()
                                        .execute()
                                        .getResult();
                                if (set.next()) {
                                    SqlManager.createUpdate()
                                            .inTable("nick")
                                            .addColumnNames(new String[]{"nickname"})
                                            .addParams(new String[]{args[0]})
                                            .addCondition("player_id", id)
                                            .build()
                                            .execute();
                                } else {
                                    String[] names = {"player_id", "nickname"};
                                    Object[] params = {id, args[0]};
                                    SqlManager.createInsert()
                                            .inTable("nick")
                                            .addColumnNames(names)
                                            .addParams(params)
                                            .build()
                                            .execute();
                                }
                                SqlManager.close();
                            }
                        };
                        addPlayerNick.runTaskAsynchronously(CharonCore.getInstance());
                        ((Player) sender).getPlayer().setDisplayName(args[0]);
                        ((Player) sender).getPlayer().setPlayerListName(args[0]);
                        refreshNick((Player) sender);
                        sender.sendMessage("§7 您的昵称已改变。");
                    }
                }
            }
        }
        return true;
    }

    public static void refreshNick(Player player){
        for (Player p : Bukkit.getOnlinePlayers()){
            if (!p.equals(player)){
                p.hidePlayer(player);
                p.showPlayer(player);
            }
        }
    }
}
